﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System.Text;
using System.Data;

namespace WCFService
{
    [ServiceContract]
    public interface IAndroidService
    {
        [OperationContract]
        [WebGet(
            UriTemplate = "GetUser/{id}",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
        UserInfo GetUserInfo(string id);

        [OperationContract]
        [WebGet(
            UriTemplate = "GetAllItems",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
        List<ItemInfo> GetAllItems();

        [OperationContract]
        [WebGet(
            UriTemplate = "GetItem/{id}",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
        ItemInfo GetItemInfoByID(string id);

        [OperationContract]
        [WebInvoke(
            Method = "POST",
            UriTemplate = "SubmitOrder",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
        bool SubmitOrder(OrderInfo orderInfo);

        [OperationContract]
        [WebGet(
            UriTemplate = "GetAllOrders",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
        List<OrderInfo> GetAllOrders();

        [OperationContract]
        [WebGet(
            UriTemplate = "GetOrder/{id}",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
        OrderInfo GetOrdersInfoByID(string id);
    }

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AndroidService : IAndroidService
    {
        public UserInfo GetUserInfo(string id)
        {
            return new UserInfo() { UserID = "admin", UserName = "admin" };
        }

        public List<ItemInfo> GetAllItems()
        {
            ItemDA itemDA = new ItemDA();
            return itemDA.GetAllItemsInfo();
        }

        public ItemInfo GetItemInfoByID(string id)
        {
            ItemDA itemDA = new ItemDA();
            return itemDA.GetItemInfoByID(id);
        }

        public bool SubmitOrder(OrderInfo orderInfo)
        {
            OrdersDA orderDA = new OrdersDA();
            if (orderDA.InsertOrderInfo(orderInfo) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public List<OrderInfo> GetAllOrders()
        {
            OrdersDA orderDA = new OrdersDA();
            return orderDA.GetAllOrdersInfo();
        }

        public OrderInfo GetOrdersInfoByID(string id)
        {
            OrdersDA orderDA = new OrdersDA();
            return orderDA.GetOrdersInfoByID(id);
        }
    }
}
